package businesslogic;

import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;

import database.OrderService;
import database.UserService;

import po.*;

import businesslogicservice.OrderBLService;

public class OrderBL extends UnicastRemoteObject implements OrderBLService{

	OrderService orderDatabase;
	
	public OrderBL(String username, String password) throws RemoteException {
		try {
			orderDatabase = new OrderService(username, password);
		} catch (Exception e) {
			
		}
	}

	public void makeNewOrder(UserPO user, SalesPO sale) throws RemoteException, SQLException {
		int orderID = sale.getSalesID();
		
		Calendar c = Calendar.getInstance();
		String date = c.get(Calendar.YEAR)+"-"+c.get(Calendar.MONTH)+"-"+c.get(Calendar.DAY_OF_MONTH)+" "+
				c.get(Calendar.HOUR_OF_DAY)+":"+c.get(Calendar.MINUTE)+":"+c.get(Calendar.SECOND);
		
		OrderStatus status = OrderStatus.ORDER;
		ArrayList<BookPO> books = sale.getBooks();
		ArrayList<Integer> numbers = sale.getNumbers();
		double totalPrice = sale.getTotalPrice();
		String name = user.getCustomerPO().getName();
		String phone = user.getCustomerPO().getPhone();
		String address = user.getCustomerPO().getAddress();
		
		OrderPO order = new OrderPO(orderID, date, status, books, numbers, totalPrice, name, phone, address);
		orderDatabase.makeNewOrder(order, user.getUsername());
	}

	public OrderPO getOrder(int orderID) throws RemoteException, SQLException {
		return orderDatabase.getOrder(orderID);
	}

	public void modifyOrder(OrderPO order) throws RemoteException, SQLException {
		orderDatabase.modifyOrder(order);
	}

	public void changeOrderStatus(int orderID, OrderStatus status) throws RemoteException, SQLException {
		OrderPO order = orderDatabase.getOrder(orderID);
		order.setStatus(status);
		orderDatabase.modifyOrder(order);
	}

}
